package org.mobile.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.mobile.logic.UserLogic;

/**
 * Servlet implementation class RemoveUser
 */
@WebServlet(name = "removeUser", urlPatterns = { "/removeUser" })
public class RemoveUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RemoveUser() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int userID= Integer.parseInt(request.getParameter("userID"));
		if(!UserLogic.getInstance().removeUser(userID)){
			request.setAttribute("error", "Błąd usuwania użytkownika!");
			request.getRequestDispatcher("errPage.jsp").forward(request, response);
		}
		else{
			request.getSession().setAttribute("users", UserLogic.getInstance().getUsers());
			request.setAttribute("message", "Użytkownik został usunięty");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		
	
	}

}
